home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / pmake / customs / os-sunos4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-15  |  7.4 KB  |  290 lines

  1. /***********************************************************************
  2.  *
  3.  * PROJECT:      PMake
  4.  * MODULE:      Customs -- SunOS 4.0 dependencies
  5.  * FILE:      os-sunos4.c
  6.  *
  7.  * AUTHOR:        Adam de Boor: Sep  9, 1989
  8.  *
  9.  * ROUTINES:
  10.  *    Name              Description
  11.  *    ----              -----------
  12.  *    OS_Init                Initialize module and return mask of criteria
  13.  *                        to be considered in determination.
  14.  *    OS_Idle                Return machine idle time, in seconds
  15.  *    OS_Load                Return machine load factor
  16.  *    OS_Swap                Return free swap space
  17.  *
  18.  * REVISION HISTORY:
  19.  *    Date      Name        Description
  20.  *    ----      ----        -----------
  21.  *    9/ 9/89      ardeb        Initial version
  22.  *
  23.  * DESCRIPTION:
  24.  *    OS-dependent functions for SunOS 4.0.
  25.  *
  26.  *    These functions are responsible for the determination of the
  27.  *    current state of the system, returning it as a set of numbers
  28.  *    in a customs-standard form, whatever form they may be in in the
  29.  *    system itself.
  30.  *
  31.  *    The format used is the same as that transmitted for the AVAIL_SET
  32.  *    RPC.
  33.  *
  34.  *     Copyright (c) Berkeley Softworks 1989
  35.  *     Copyright (c) Adam de Boor 1989
  36.  *
  37.  *     Permission to use, copy, modify, and distribute this
  38.  *     software and its documentation for any non-commercial purpose
  39.  *    and without fee is hereby granted, provided that the above copyright
  40.  *     notice appears in all copies.  Neither Berkeley Softworks nor
  41.  *     Adam de Boor makes any representations about the suitability of this
  42.  *     software for any purpose.  It is provided "as is" without
  43.  *     express or implied warranty.
  44.  *
  45.  *
  46.  ***********************************************************************/
  47. #ifndef lint
  48. static char *rcsid =
  49. "$Id: os-sunos4.c,v 1.2 89/10/11 17:22:23 adam Exp $";
  50. #endif lint
  51.  
  52. #include    "customsInt.h"
  53.  
  54. #undef NULL
  55.  
  56. #include    <sys/types.h>
  57. #include    <sys/param.h>
  58. #include    <nlist.h>
  59. #include    <fcntl.h>
  60. #include    <sys/stat.h>
  61. #include    <stdio.h>
  62. #include    <vm/anon.h>
  63. #include    <kvm.h>
  64.  
  65. static struct nlist kAddrs[] = {
  66. {    "_avenrun"  },        /* Load averages                    */
  67. #define AVENRUN        0
  68. {    "_anoninfo" },        /* Swap space stats                    */
  69. #define ANONINFO    1
  70. {    ""          }
  71. };
  72.  
  73. static kvm_t        *kmem;  /* Token for referencing the kernel stuff */
  74. static int          kbd;    /* Stream open to keyboard device (idle time) */
  75.  
  76. static u_long        avenrun[3];    /* Load averages */
  77.  
  78.  
  79. /***********************************************************************
  80.  *                OS_Init
  81.  ***********************************************************************
  82.  * SYNOPSIS:        Initialize this module
  83.  * CALLED BY:        Avail_Init
  84.  * RETURN:        Mask of bits indicating what things we can check
  85.  * SIDE EFFECTS:    kmem and kbd are set. kAddrs filled in.
  86.  *
  87.  * STRATEGY:
  88.  *
  89.  * REVISION HISTORY:
  90.  *    Name    Date        Description
  91.  *    ----    ----        -----------
  92.  *    ardeb    9/10/89        Initial Revision
  93.  *
  94.  ***********************************************************************/
  95. int
  96. OS_Init()
  97. {
  98.     int    retMask = AVAIL_EVERYTHING;
  99.  
  100.     /*
  101.      * Open the kernel. If that fails, kvm_open will have given the
  102.      * error for us, so we can just exit.
  103.      */
  104.     kmem = kvm_open(NULL, NULL, NULL, O_RDONLY, "customs");
  105.     if (kmem == NULL) {
  106.     exit(1);
  107.     }
  108.  
  109.     /*
  110.      * Locate the other structures in the kernel that we need.
  111.      */
  112.     switch (kvm_nlist(kmem, kAddrs)) {
  113.     case -1:
  114.         printf("couldn't locate kernel symbols\n");
  115.         exit(1);
  116.     case 0:
  117.         /*
  118.          * No symbols unfound -- this is good.
  119.          */
  120.         break;
  121.     default:
  122.         if (kAddrs[AVENRUN].n_type == 0) {
  123.         /*
  124.          * Couldn't find _avenrun symbol, so can't determine load
  125.          * average.
  126.          */
  127.         retMask &= ~AVAIL_LOAD;
  128.         }
  129.         if (kAddrs[ANONINFO].n_type == 0) {
  130.         /*
  131.          * If couldn't find _anoninfo, we can't figure out the
  132.          * swap space situation.
  133.          */
  134.         retMask &= ~AVAIL_SWAP;
  135.         }
  136.         break;
  137.     }
  138.     
  139.     /*
  140.      * Try and open the keyboad so we can just do an fstat on it (rather
  141.      * than a full stat). This also tells us if the thing exists.
  142.      */
  143.     kbd = open("/dev/kbd", O_RDONLY, 0);
  144.     if (kbd < 0) {
  145.     /*
  146.      * No keyboard, no idle time.
  147.      */
  148.     retMask &= ~AVAIL_IDLE;
  149.     }
  150.  
  151.     return(retMask);
  152. }
  153.  
  154. /***********************************************************************
  155.  *                OS_Idle
  156.  ***********************************************************************
  157.  * SYNOPSIS:        Find the idle time of the machine
  158.  * CALLED BY:        Avail_Local
  159.  * RETURN:        The number of seconds for which the machine has been
  160.  *                idle.
  161.  * SIDE EFFECTS:    None
  162.  *
  163.  * STRATEGY:
  164.  *    Locate the access time for the keyboard device and subtract it
  165.  *    from the current time to obtain the number of seconds the
  166.  *    keyboard has been idle. The assumption is that the keyboard
  167.  *    device's idle time reflects that of the machine. This does not
  168.  *    take into account rlogin connections, or non-Sun systems that
  169.  *    are workstations but don't have a keyboard device.
  170.  *
  171.  *
  172.  * REVISION HISTORY:
  173.  *    Name    Date        Description
  174.  *    ----    ----        -----------
  175.  *    ardeb    9/10/89        Initial Revision
  176.  *
  177.  ***********************************************************************/
  178. int
  179. OS_Idle()
  180. {
  181.     struct stat        kbStat;
  182.     struct timeval  now;
  183.  
  184.     fstat (kbd, &kbStat);
  185.     gettimeofday (&now, (struct timezone *)0);
  186.     
  187.     return (now.tv_sec - kbStat.st_atime);
  188. }
  189.  
  190.  
  191. /***********************************************************************
  192.  *                OS_Swap
  193.  ***********************************************************************
  194.  * SYNOPSIS:        Find the percentage of the system's swap space that
  195.  *                isn't being used.
  196.  * CALLED BY:        Avail_Local
  197.  * RETURN:        The percentage of free swap space
  198.  * SIDE EFFECTS:    None
  199.  *
  200.  * STRATEGY:
  201.  *    The number of blocks is kept in the "anoninfo" structure in the
  202.  *    kernel. The numbers are actually given in pages, but we only
  203.  *    deal with percentages, so we don't care.
  204.  *
  205.  * REVISION HISTORY:
  206.  *    Name    Date        Description
  207.  *    ----    ----        -----------
  208.  *    ardeb    9/10/89        Initial Revision
  209.  *
  210.  ***********************************************************************/
  211. int
  212. OS_Swap()
  213. {
  214.     struct anoninfo ani;
  215.  
  216.     if (kvm_read(kmem,
  217.          kAddrs[ANONINFO].n_value,
  218.          &ani,
  219.          sizeof(ani)) != sizeof(ani))
  220.     {
  221.     /*
  222.      * Couldn't find out -- assume worst case.
  223.      */
  224.     return(0);
  225.     } else {
  226.     /*
  227.      * Convert free to percentage and return it.
  228.      */
  229.     return((ani.ani_free * 100) / ani.ani_max);
  230.     }
  231. }
  232.  
  233.  
  234. /***********************************************************************
  235.  *                OS_Load
  236.  ***********************************************************************
  237.  * SYNOPSIS:        Return the current load average in standard form
  238.  * CALLED BY:        Avail_Local
  239.  * RETURN:        The current load as a 32-bit fixed-point number
  240.  * SIDE EFFECTS:    None
  241.  *
  242.  * STRATEGY:
  243.  *
  244.  * REVISION HISTORY:
  245.  *    Name    Date        Description
  246.  *    ----    ----        -----------
  247.  *    ardeb    9/10/89        Initial Revision
  248.  *
  249.  ***********************************************************************/
  250. unsigned long
  251. OS_Load()
  252. {
  253.     unsigned long   result;
  254.     unsigned long   avenrun[3];
  255.  
  256.     if (kvm_read(kmem,
  257.          kAddrs[AVENRUN].n_value,
  258.          avenrun,
  259.          sizeof(avenrun)) != sizeof(avenrun))
  260.     {
  261.     return(0);
  262.     }
  263.  
  264. #define CVT(v)    ((double)(v)/FSCALE)*LOADSCALE
  265.  
  266. #ifdef ALL_LOAD
  267.     /*
  268.      * Find largest of the three averages and return that
  269.      */
  270.     if (avenrun[0] > avenrun[1]) {
  271.     if (avenrun[0] > avenrun[2]) {
  272.         result = CVT(avenrun[0]);
  273.     } else {
  274.         result = CVT(avenrun[2]);
  275.     }
  276.     } else if (avenrun[1] > avenrun[2]) {
  277.     result = CVT(avenrun[1]);
  278.     } else {
  279.     result = CVT(avenrun[2]);
  280.     }
  281. #else
  282.     /*
  283.      * Just return the 1-minute average.
  284.      */
  285.     result = CVT(avenrun[0]);
  286. #endif
  287.  
  288.     return(result);
  289. }
  290.